package org.web.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.net.util.AnalysisContentUtil;
import org.net.util.ComplexExportExcelClient;
import org.net.util.FetchUtil;
import org.net.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.web.core.Page;
import org.web.core.SimpleController;
import org.web.core.service.CommentResourcesService;
import org.web.core.service.ResourceService;
import org.web.entity.CommentResources;
import org.web.entity.Resources;
import org.web.schedule.CallAutoExecuteFetchSiteJob;



@SuppressWarnings("serial")
@Controller
public class ResourceManageAction extends SimpleController {
	
	 private static Logger log = LoggerFactory.getLogger(ResourceManageAction.class);
	 
	 private HashSet<String> ids = new HashSet<String>();
	
	@Resource(name = "resourceService")
    private ResourceService resourceService;
	
	@Resource(name = "commentResourcesService")
    private CommentResourcesService commentResourcesService;
	
	@RequestMapping(value = "/list.shtml")
    public String resource(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
	    try{
	    	String page = request.getParameter("pageNo");
	    	String type = request.getParameter("type");
	    	if(StringUtil.isEmpty(page))   page ="1";
	    	Map<String, Object> parameters =new HashMap();
	    	if(StringUtil.isEmpty(type))    type = "leave";
	    	Page resource = new Page();
	    	if("leave".equalsIgnoreCase(type))  {
	    		parameters.put("isUsed", "N");
	    		resource = commentResourcesService.findByPage(new Page(Integer.parseInt(page),100), parameters);
	    	}
	    	if("source".equalsIgnoreCase(type)) {
	    		parameters.put("isCrawled", "N"); 
	    		parameters.put("fetchSource","yes");
	    	    resource = resourceService.findByPage(new Page(Integer.parseInt(page),1000), parameters);
	    	}
	    	
	    	if("export".equalsIgnoreCase(type)) {
	    		try{
	    		parameters.put("isUsed", "N");
	    		resource = commentResourcesService.findByPage(new Page(Integer.parseInt(page),100), parameters);
	    		List<CommentResources> crs =resource.getResult();
	    		for(CommentResources cr : crs){
	    			cr.setIsUsed("E");
	    			commentResourcesService.update(cr);
	    		}
	    		OutputStream os = response.getOutputStream();
	            response.reset();
	            response.setContentType("application/vnd.ms-excel");
	            String s = "attachment; filename=LeaveComment-" + page + ".xls";
	            response.setHeader("Content-Disposition", s);
	            ComplexExportExcelClient.responseSiteInfo(os,resource.getResult());
	            os.flush();
	            os.close();
	    		}catch(Exception e){
	    			e.printStackTrace();
	    		}
	            return null;
	    	}
	    	
	    	if("source".equalsIgnoreCase(type)) {
	    		List<Resources> sources = resource.getResult();
	    		HashSet<String> autoids = CallAutoExecuteFetchSiteJob.getIds();
	    		for(Resources rs: sources){
	    			if(ids.contains(rs.getId().toString()) || autoids.contains(rs.getId().toString())){
	    				rs.setIsCrawled("Y");
	    			}
	    		}
	    	}
	    	model.addAttribute(resource);
	    	if("source".equalsIgnoreCase(type)) { 
	    		return "crawl"; 
	    	}
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "resource";
    }
	
	@RequestMapping(value = "/crawl.shtml" , method =RequestMethod.GET)
	public String crawl(HttpServletRequest request, HttpServletResponse response,ModelMap model){
		String target = request.getParameter("url");
		String resourceId = request.getParameter("resourceId");
		if(!StringUtil.isEmpty(resourceId)){
		    Resources res = resourceService.getById(Long.valueOf(resourceId));
		    if(res!=null)       target = "www."+AnalysisContentUtil.getItsDomainName(res.getUrl());
		    ids.add(resourceId);
		}
		if(StringUtil.isEmpty(target))     return null;
		final String url = target;
		final String resId = resourceId;
		new Thread(){
			public void run(){
			try{
				long start = System.currentTimeMillis();
				String targetUrl ="http://siteexplorer.search.yahoo.com/search?p="+url+"&fr=sfp&bwm=i";
				String responseBody = FetchUtil.getHTMLResponseBodyAsString(targetUrl);
				if(!StringUtil.isEmpty(responseBody)){
					Pattern p = null;
					Matcher m = null;
					StringBuffer sb0 = new StringBuffer();
					String regex ="<span class=\"result\"><a.*href=\"(.*)\".*>(.+?)</a>(.*)<p class=\"details\">(.*)</p><p class=\"info\">(.*)<span class=\"size\">(.*)</span></p></span>";
					p = Pattern.compile(regex);
					m = p.matcher(responseBody);
					while (m.find()) {
						sb0.append(m.group());
					}
					//所有前100条链接
					List<String> sites = AnalysisContentUtil.fetchAllHrefFromYahooSite(sb0.toString());
					int foreignLinkCount =0;
					for(String st:sites){
						try{
						//打开页面并判断页面上所存在的链接数
						foreignLinkCount = AnalysisContentUtil.countAllHrefExceptSelfInputSteam(st,resourceService);
						if(foreignLinkCount>0 && foreignLinkCount<1000){ // 当前页面不是本站的链接数量
							String html = FetchUtil.getHTMLResponseBodyAsString(st);
						    if(AnalysisContentUtil.canLeaveComment(html.toString())){ //是否有留言标识
						      log.info("find leave comment："+st+"\r\n");
						      this.currentThread().sleep(100);
							  CommentResources cr = new CommentResources();
							  cr.setCreatedDate(new Date());
							  cr.setFromWhere("link:"+url);
							  cr.setIsUsed("N");
							  cr.setUrl(st);
							  commentResourcesService.save(cr);
						    }
						}
						}catch(Exception e) { e.printStackTrace(); continue; }
					}
				}
				if(!StringUtil.isEmpty(resId)){
					Resources rs = resourceService.getById(Long.parseLong(resId));
					rs.setIsCrawled("Y");
					rs.setUpdatedDate(new Date());
					resourceService.update(rs);
				}
				long end = System.currentTimeMillis();
				log.info("crawl "+url+" cost times："+(end-start));
				}catch(Exception e) { e.printStackTrace();}
			}
		}.start();
		try {
			response.sendRedirect("/list.shtml?type=source");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/inlink.shtml" , method =RequestMethod.GET)
	public String inlink(HttpServletRequest request, HttpServletResponse response,ModelMap model){
		String url = request.getParameter("url");
		if(!StringUtil.isEmpty(url)){
		    return "0";
		}
		try{
		String targetUrl ="http://siteexplorer.search.yahoo.com/search?p="+url+"&fr=sfp&bwm=i";
		String responseBody = FetchUtil.getHTMLResponseBodyAsString(targetUrl);
		if(!StringUtil.isEmpty(responseBody)){
			Pattern p = null;
			Matcher m = null;
			StringBuffer sb0 = new StringBuffer();
			String regex ="</li><li><span class=\"btn\">Inlinks(.*)</span></li></ul></div><div id=\"inlink-options\" class=\"opt-list opt-right\"><form method=\"get\" action=\"http://siteexplorer.search.yahoo.com/search\"><label for=\"bwmo\" class=\"optdesc\">Show Inlinks:</label>";
			p = Pattern.compile(regex);
			m = p.matcher(responseBody);
			while (m.find()) {
				sb0.append(m.group(1));
			}
			
			System.out.println(sb0.toString());
			return sb0.toString();
		}
		}catch(Exception e){ e.printStackTrace(); return "0";}
		return null;
		
	}
	
	
	@RequestMapping(value = "/domain.shtml" , method =RequestMethod.GET)
	public String domain(HttpServletRequest request, HttpServletResponse response,ModelMap model){
		String url = request.getParameter("url");
		if(!StringUtil.isEmpty(url)){
		    return "0";
		}
		try{
		String targetUrl ="http://www.whois-search.com/whois/"+url;
		String responseBody = FetchUtil.getHTMLResponseBodyAsString(targetUrl);
		if(!StringUtil.isEmpty(responseBody)){
			Pattern p = null;
			Matcher m = null;
			StringBuffer sb0 = new StringBuffer();
			String regex ="Creation Date:(.*)";
			p = Pattern.compile(regex);
			m = p.matcher(responseBody);
			while (m.find()) {
				sb0.append(m.group(1));
			}
			
			return sb0.toString();
		}
		}catch(Exception e){ e.printStackTrace(); return "0";}
		return null;
		
	}
	

}
